package com.csdj.t309tgspringboot.controller;

import com.csdj.t309tgspringboot.common.WebResult;
import com.csdj.t309tgspringboot.entity.Post;
import com.csdj.t309tgspringboot.service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/post")
public class PostController {
    @Resource
    private PostService postService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private WebResult webResult ;
    private Post post;

    @ResponseBody
    @GetMapping("/insertPsst")
    public WebResult insertPost(@RequestParam(value = "title",required = false) String title,
                                @RequestParam(value = "content",required = false) String content){
        post = new Post();
        post.setTitle(title);
        post.setContent(content);
        post.setUserId(Integer.parseInt(stringRedisTemplate.opsForValue().get("id")));
        int num = postService.insertPost(post);
        if(num>0){
            webResult = new WebResult();
            webResult.putData("num",num);
            return webResult;
        }
        return null;
    }

    @ResponseBody
    @GetMapping("/selPost")
    public WebResult selPost(@RequestParam(value = "userId",required = false) Integer userId,
                             @RequestParam(value = "id",required = false) Integer id){
        post = postService.selPost(userId,id);
        webResult = new WebResult();
        webResult.putData("post",post);
        return webResult;
    }

    @ResponseBody
    @GetMapping("/selPosts")
    public WebResult selPosts(@RequestParam(required = true, defaultValue = "1") Integer page){
        PageHelper.startPage(page,1);
        List<Post> posts = postService.selPosts();
        PageInfo<Post> p = new PageInfo<>(posts);
        webResult = new WebResult();
        webResult.putData("posts",posts).putData("p",p);
        return webResult;
    }

    private String num1 =null;
    private Integer sum =0;
    @ResponseBody
    @GetMapping("/dianzan")
    public WebResult dianZan(@RequestParam(value = "userId",required = false) Integer userId,
                             @RequestParam(value = "id",required = false) String id){
        webResult = new WebResult();
        num1 = stringRedisTemplate.opsForValue().get(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id")));
        if(num1!=null){
            if(num1.equals("0")){
                stringRedisTemplate.opsForValue().set(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id")),"1");
                num1 = "1";
                stringRedisTemplate.opsForValue().increment("post"+Integer.toString(userId)+id,1);
            }else{
                stringRedisTemplate.opsForValue().set(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id")),"0");
                num1="0";
                stringRedisTemplate.opsForValue().increment("post"+Integer.toString(userId)+id,-1);
            }
        }else {
            stringRedisTemplate.opsForValue().set(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id")),"1");
            num1 = "1";
            stringRedisTemplate.opsForValue().increment("post"+Integer.toString(userId)+id,1);
        }
        webResult.putData("num1",num1).putData("sum",stringRedisTemplate.opsForValue().get("post"+Integer.toString(userId)+id));
        return webResult;
    }

    @ResponseBody
    @GetMapping("/dd")
    public WebResult dd(@RequestParam(value = "id",required = false)Integer id,
                     @RequestParam(value = "userId",required = false) Integer userId){
        webResult = new WebResult();
        if(stringRedisTemplate.opsForValue().get("post"+Integer.toString(userId)+id)!=null){
            webResult.putData("sum",stringRedisTemplate.opsForValue().get("post"+Integer.toString(userId)+id));
        }else {
            webResult.putData("sum","0");
        }
        if(stringRedisTemplate.opsForValue().get(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id")))!=null){
            webResult.putData("num1",stringRedisTemplate.opsForValue().get(Integer.toString(userId)+id+Integer.parseInt(stringRedisTemplate.opsForValue().get("id"))));
        }else {
            webResult.putData("num1","0");
        }
        return webResult;
    }
}
